﻿//测试连表查询与分解查询在不同环境下表现的差别
//mock测试数据主表100条 二级表1000条 三级表10000条
//用ef执行，stopwatch统计执行用时

//mock数据
using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
using 测试连表查询与分解查询;
using 测试连表查询与分解查询.model;

DefaultDbContext _db = new DefaultDbContext();
//for(var i = 1; i <= 100; i++)
//{
//    Level1_Model obj1=new Level1_Model();
//    obj1.Guid1=Guid.NewGuid().ToString();
//    obj1.Name = "Name" + i;
//    obj1.Description = "Description" + i;
//    _db.Level1_Models.Add(obj1);
//    for (var j = 1; j <= 10; j++)
//    {
//        Level2_Model obj2=new Level2_Model();
//        obj2.Guid2=Guid.NewGuid().ToString();
//        obj2.Name = "Name" + j;
//        obj2.Description = "Description" + j;
//        obj2.Guid1 = obj1.Guid1;
//        _db.Level2_Models.Add(obj2);
//        for (var z = 1; z <= 10; z++)
//        {
//            Level3_Model obj3=new Level3_Model();
//            obj3.Guid3=Guid.NewGuid().ToString();
//            obj3.Name = "Name" + z;
//            obj3.Description = "Description" + z;
//            obj3.Guid1 = obj1.Guid1;
//            obj3.Guid2 = obj2.Guid2;
//            _db.Level3_Models.Add(obj3);
//        }
//    }
//}
//插入

var guid = "031863be-2e7f-48de-a16d-f00a5e984a09";
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var l1 = await _db.Level1_Models.Where(x => x.Guid1 == guid).FirstAsync();
var l2 = await _db.Level2_Models.Where(x => x.Guid1 == guid).ToListAsync();
var l3 = await _db.Level3_Models.Where(x => x.Guid1 == guid).ToListAsync();

ResultModel resultModel = new ResultModel();
resultModel.Name = l1.Name;
resultModel.Description = l1.Description;
resultModel.Guid = l1.Guid1;
List<ResultModel> l2_ResultModel = new List<ResultModel>();
l2.ForEach(x =>
{
    ResultModel obj = new();
    obj.Name = x.Name;
    obj.Description = x.Description;
    obj.Guid = x.Guid1;
    List<ResultModel> l3_ResultModel = new List<ResultModel>();
    l3.Where(x => x.Guid2 == x.Guid2).ToList().ForEach(z =>
    {
        ResultModel obj2 = new();
        obj2.Name = z.Name;
        obj2.Description = z.Description;
        l3_ResultModel.Add(obj2);
    });
    obj.Children = l3_ResultModel;
    l2_ResultModel.Add(obj);
});
stopwatch.Stop();
Console.WriteLine("efcore查询耗时:" + stopwatch.ElapsedMilliseconds);

//获取三级表0073875d-1e61-4a15-bc13-89fe26a602fe数据
//分解查询